package rhinestone.Model;

import java.util.ArrayList;
import java.util.Iterator;
import java.io.Serializable;

/**
 * The container for storing all Sales.
 *
 * @author Group 2
 * @version 2010.12.23
 */
public class SaleContainer implements Serializable {
	private ArrayList<Sale> sales;
	private static SaleContainer instance;
	private boolean changed;

	private SaleContainer() {
		sales = new ArrayList<Sale>();
		changed = false;
	}

	public static SaleContainer getInstance() {
		if(instance == null)
			instance = new SaleContainer();
		return instance;
	}

	public void addSale(Sale sale) {
		sales.add(sale);
		changed = true;
	}

	public int getSize() {
		return sales.size();
	}

	public Iterator<Sale> getSales(Staff staff) {
		ArrayList<Sale> foundSales = new ArrayList<Sale>();
		for(int i = 0; i < sales.size(); i++) {
			Sale sale = sales.get(i);
			if(sale.getStaff().getCpr().equals(staff.getCpr()))
				foundSales.add(sale);
		}
		return foundSales.iterator();
	}

	public Iterator<Sale> getSales(Customer customer) {
		ArrayList<Sale> foundSales = new ArrayList<Sale>();
		for(int i = 0; i < sales.size(); i++) {
			Sale sale = sales.get(i);
			if(sale.getCustomer().getCpr().equals(customer.getCpr()))
				foundSales.add(sale);
		}
		return foundSales.iterator();
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}

	public boolean isChanged() {
		return changed;
	}
}